home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / PRG / Prograph Reference Manual.sit / Prograph Reference Manual / Prograph Reference 8-End.rsrc / TEXT_146.txt < prev    next >
Text File  |  1995-10-26  |  18KB  |  552 lines

  1.  
  2. t    Writing XCode *574*
  3.  
  4. This section describes how to write XCode. XCode allows you to access and define Prograph language elements in C. With XCode you can define and access persistents, class attributes, universal methods, simple class methods, class initialization methods and attribute get and set methods. You can also access (but not define) classes and instance attributes. XCode is compatible with compiled Prograph code but cannot be run by the Prograph interpreter.
  5.  
  6. A set of naming conventions is used to map between Prograph code and C code. For example, to define a universal method, myCmethod, in C, you would create a C function whose name was U_myCmethod. Conversely, to call, from C, a universal method, myPmethod, which is defined in Prograph, you would call the function U_myPmethod.
  7.  
  8.  
  9. _______________________________________
  10. NOTE:    Names are case sensitive.  Non-alphanumeric characters are represented by their ASCII code, preceded and followed by an underscore.  For instance, U_2B_1 represents the universal method +1.
  11.  
  12.  
  13. _______________________________________
  14. NOTE:    XCode cannot be run by the Prograph interpreter.  The compiled? primitive can be used to prevent the interpreter from attempting to execute any references to XCode. 
  15.  
  16.  Function Return Values *575*
  17.  
  18. When a method completes without error, its C function should return PCF_TRUE for its integer return parameter.  The only exception is a boolean method with no output root; such a method, if it completes successfully, conveys its boolean result by passing either PCF_TRUE or PCF_FALSE.
  19.  
  20. In compiled code values such as PRIMERR_ARITY, PRIMERR_VALUE and PRIMERR_TYPE should never be returned by methods. 
  21.  
  22.  Prograph Language Elements *575*
  23.  
  24. This section describe how to define and access each of the Prograph language elements in C. The conventions described in this section apply equally to THINK C and MPW C. These conventions will be slightly different if you are using the THINK C object extensions. These differences are discussed in a separate section.
  25.  
  26. Persistents *575*
  27.  
  28. In C, persistents are represented as global variables whose names begin with P_. The persistent, 窶徇yPersist窶, defined in Prograph, is represented in C as a global variable P_myPersist.
  29.  
  30.  C Representation:  
  31.  
  32.   P_id     
  33.  
  34.  
  35.  Example:   
  36.  
  37.   /* Assume myPersist exists in the Prograph source */
  38.   
  39.   extern C_integer *P_myPersist;
  40.   
  41.   DecUse( P_myPersist);    /* remove the existing value */
  42.   P_myPersist = MakeC_integer( 12 );    /* set to a new value */
  43.   ...
  44.  
  45.  Example:   
  46.  
  47.   C_integer *P_myPersist;     /* defines myPersist */
  48.   ...
  49.  
  50. Universal Methods and Primitives *576*
  51.  
  52. In C universal methods and primitives are declared as functions of the form U_id which return a Nat2 value. The parameters of the function correspond to the roots and terminals of a Prograph operation. 
  53.  
  54. No distinction is made between universal methods and primitives by the Prograph compiler. A universal method, 窶徭tart-up窶, defined in Prograph, is represented in C as a function U_start_2D_up. The primitive "join" is represented as U__22_join_22_. 
  55.  
  56. You must use the SETARITY macro (described in the Arity Macros section) to set the arity before calling any method or primitive which has variable arity. 
  57.  
  58.  C Representation:  
  59.  
  60.   U_id     
  61.  
  62.  
  63.  Example:   
  64.  
  65.   /* Assume string1 is "Elvis " and string2 is "lives" */
  66.   /* string3 is uninitialized */
  67.   
  68.   C_string *string1, *string2, *string3;
  69.  
  70.   SET_ARITY( 2, 1 );     /* "join" has variable arity */
  71.   U__22_join_22_( string1, string2, &string3 );
  72.  
  73.   /* string3 is now "Elvis lives" */
  74.   ...
  75.  
  76.  Example:   
  77.  
  78.   /* The universal method swap-inputs will be callable */
  79.   /* from a compiled Prograph program */
  80.  
  81.   Nat2 U_swap_2D_inputs( in1, in2, out1, out2 )
  82.   C_object *in1, *in2;
  83.   C_object **out1, **out2;
  84.   {
  85.    IncUse( in1 );
  86.    IncUse( in2 );
  87.    *out1 = in2;
  88.    *out2 = in1;
  89.  
  90.    return PCF_TRUE;
  91.   } 
  92.  
  93. Class Identifiers *576*
  94.  
  95. Every Prograph class has an associated class identifier. Class identifiers are global variables of the C type Class and of the form C_id__. The class identifier of a Prograph class, 窶弃erson窶, is represented in C as a global variable C_Person__. 
  96.  
  97. Class identifiers are used with the supplied functions Member, Bless, New, NewN and the macro INCLASS to determine or set the class of a data item. All of these functions require the address  of a class identifier. 
  98.  
  99. Prograph classes and class identifiers cannot be defined in C. 
  100.  
  101.  C Representation:  
  102.  
  103.   C_id__ 
  104.   
  105.  
  106.  Example:   
  107.  
  108.   /* Use the macro INCLASS to test if */
  109.   /* anInstance is of class Person */
  110.  
  111.   C_object *anInstance;
  112.   extern Class C_Person__;
  113.   
  114.   if ( INCLASS( anInstance, &C_Person__ ) );    
  115.    ...
  116.  
  117. Default Instances *577*
  118.  
  119. Every Prograph class has a default instance which is accessible in C as a global variable of the form C_class__A_default. The default instance of a class, 窶弃erson窶, defined in Prograph, is represented in C as a global variable C_Person__A_Default, of the type C_Person*.
  120.  
  121.  C Representation:  
  122.  
  123.   C_class__A_default
  124.   
  125.  Example:   
  126.  
  127.   /* Use the Duplicate function to create a new instance*/
  128.   /* of the class Person */
  129.  
  130.   C_object *anInstance;
  131.   
  132.   
  133.   anInstance = Duplicate( C_Person__A_default, DEEP_COPY);    
  134.    ...
  135.  
  136. Class Attributes *577*
  137.  
  138. Class attributes are represented in C as global variables of the form C_class__A_id. Class attributes can be defined in C. The class attribute, 窶弃erson List窶, of the class, 窶弃erson窶, defined in Prograph, is represented in C as a global variable 
  139. C_Person__A_Person_20_List.
  140.  
  141.  C Representation:  
  142.  
  143.   C_class__A_id
  144.  
  145.  
  146.  Example:   
  147.  
  148.   /* Get class attribute "Person List" of class Person*/
  149.   C_Person *myPerson;
  150.   C_list *myList;    
  151.  
  152.   myList = C_Person__A_Person_20_List;
  153.   ...
  154.  
  155.  Example:   
  156.  
  157.   /* Define class attribute "First Person" of class Person */
  158.   /* This class attribute will be available in Prograph */
  159.  
  160.   C_Person *C_Person__A_First_20_Person;
  161.   ...
  162.  
  163. Instance Attributes *578*
  164.  
  165. Instance attributes can be accessed but not defined in C. To access the instance attributes of a Prograph class you define a structure in C which parallels the Prograph class definition. The C structure must begin with a two-byte type field followed by four-byte save and use fields. The remaining fields (four-bytes each) correspond in order to the instance attributes of the Prograph class. The structure name should be of the form C_id. Field names are arbitrary but it is good practice to use the same names as used in the Prograph class. 
  166.  
  167.  Example:  
  168.  
  169.   /* Assume a Prograph class Person with three attributes */
  170.   
  171.   /* Structure Declaration  */
  172.   typedef struct 
  173.   {      
  174.    Int2      type;    
  175.    Nat4      save;    
  176.    Nat4      use;    
  177.    C_string     *name;
  178.    C_string     *address;
  179.    C_integer    *age;
  180.   } *C_Person;
  181.  
  182.   /* Accessing attributes */
  183.   C_string *thisName;
  184.   C_Person    *aPerson;
  185.  
  186.   thisName = (**aPerson).name;
  187.   ...
  188.  
  189. Class Methods and Selectors *578*
  190.  
  191. In Prograph there are four types of class method:   simple (plain), initialization, get and set. Class methods are represented as C functions with names of the form:   C_class__M_id (simple), C_class__N_ (initialization), C_class__G_id (get), and C_class__S_id (set). In C, two other types of class methods may be defined:   default get methods and default set methods which have the forms C_class__g_id and C_class__s_id respectively. 
  192.  
  193. Class methods can be called directly or through what are known as method selectors. A method selector is a C function with the same name as a class method function preceded by an underscore. For example, the function name of a simple method selector has the form _C_class__M_id.
  194.  
  195. A method selector searches through the class hierarchy for the named method and then calls that method. The search starts at the class of the selector窶冱 first parameter and proceeds upward through the class hierarchy. If no method is found an error dialog will be displayed. 
  196.  
  197. For example, assume an instance of class dog is passed as the first parameter to the selector _C_animal__M_talk. If the class dog has a talk method, the selector will call that method. If the class dog does not have a talk method, the selector will examine each of dog窶冱 ancestor classes, in turn, until a talk method is found. If none of the ancestor classes has a talk method an error dialog is displayed.
  198.  
  199. Note that the class which is part of the selector name, animal in the example above, is not used in the search. (In future releases of the compiler this class name will be used to optimize method selection.) The class name should correspond to the declared class of the first parameter. For example:   
  200.  
  201.   /* class name animal is used because */
  202.   /* myAnimal is declared to be of class animal */
  203.  
  204.   C_animal *myAnimal;
  205.  
  206.   _C_animal__M_talk( myAnimal );
  207.   ...
  208.  
  209. Get and Set method selectors behave slightly differently when no method is found in the class hierarchy. The search is repeated, this time for the default get or set method. 
  210.  
  211. Selectors never have to be defined by the user - they are automatically created by the Prograph compiler. 
  212.  
  213. Simple Class Methods *579*
  214.  
  215. Simple class methods are represented as C functions with names of the form C_class__M_id. The simple class method 窶彙irth窶 of the class 窶弃erson窶, defined in Prograph, is represented in C as a function C_Person__M_birth.
  216.  
  217.  C Representation:  
  218.  
  219.   C_class__M_id
  220.  
  221.  Selector:       
  222.  
  223.   _C_class__M_id      
  224.  
  225.  Example:   
  226.  
  227.   /* Call the method birth of the class Person */
  228.   /* Assume birth has one input and one output */
  229.  
  230.   C_Person__M_birth( input, &output);    
  231.   ...
  232.  
  233.  Example:  
  234.  
  235.   /* Assume the class Person has been defined in Prograph */
  236.   /* Define the simple method 窶廣dd Person窶 in C */
  237.   
  238.   Nat2 C_Person__M_Add_20_Person( input )     
  239.   C_Person    *input;
  240.   { 
  241.    ...      /* Do Stuff */
  242.    return PCF_TRUE;
  243.   }
  244.  
  245. Initialization Class Methods *580*
  246.  
  247. A class initialization method is represented as a C function with a name of the form C_class__N_. Initialization methods always have one input and one output. The input value should be passed through as output. The initialization method of the class, 窶弃erson窶, defined in Prograph, is represented in C as a function C_Person__N_.
  248.  
  249. To reproduce the function of a Prograph instance generator, first create an instance by making a deep copy of the default instance then call the selector of the initialization method. 
  250.  
  251.  C Representation:  
  252.  
  253.   C_class__N_      
  254.  
  255.  Selector:  
  256.  
  257.   _C_class__N_      
  258.  
  259.  Example:  
  260.  
  261.   /* Call the initialization method of class Person */
  262.   C_Person *myPerson;
  263.  
  264.   C_Person__N_( New( C_Person__ ), &myPerson );
  265.   ...
  266.  
  267.  Example:  
  268.  
  269.   /* Assume the class Person has been defined in Prograph */
  270.   /* Define an initialization method for the class Person */
  271.   
  272.   Nat2 C_Person__N_( input, output )     
  273.   C_Person *input;
  274.   C_Person **output;
  275.   { 
  276.    ...          /* Do initialization stuff */
  277.    IncUse( input );
  278.    *output = input;
  279.    return PCF_TRUE; 
  280.   }
  281.  
  282. Get Methods *581*
  283.  
  284. Get methods are represented as C functions with names of the form C_class__G_id. The get method of the attribute, 窶彗ge窶, of the class, 窶弃erson窶, defined in Prograph, is represented in C as a function C_Person__G_age.
  285.  
  286. Get methods always have one input and two outputs. The input value should be passed through as the first output. The second output should be the value of the attribute. 
  287.  
  288.  C Representation:  
  289.  
  290.   C_class__G_id    
  291.    
  292.  Selector:   
  293.  
  294.   _C_class__G_id      
  295.   
  296.  Example:   
  297.  
  298.   /* Call the get method for attribute age of class Person */
  299.   C_Person *myPerson, *theInst;
  300.   C_integer *age;    
  301.   
  302.   C_Person__G_age( myPerson, &theInst, &age );
  303.   
  304.  Example:  
  305.  
  306.   /* Assume a Prograph class Person with three attributes */
  307.   /* C Class declaration */
  308.  
  309.   typedef struct 
  310.   {      
  311.    Int2      type;    
  312.    Nat4      save;    
  313.    Nat4      use;    
  314.    C_string     *name;
  315.    C_string     *address;
  316.    C_integer    *age;
  317.   } *C_Person;
  318.  
  319.   /* Get Method declaration */
  320.  
  321.   Nat2 C_Person__G_age( input, output1, output2 )    
  322.   C_Person *input;
  323.   C_Person **output1;
  324.   C_integer **output2;
  325.   {
  326.    ...         /* Do Get stuff */
  327.    IncUse( input);
  328.    IncUse( (**input).age );
  329.    *output1 = input;
  330.    *output2 = (**input).age;
  331.    return PCF_TRUE;
  332.   }
  333.  
  334. Set Methods *582*
  335.  
  336. Set methods are represented as C functions with names of the form C_class__S_id. The set method of the attribute 窶彗ge窶 of the class 窶弃erson窶 defined in Prograph, is represented in C as a function C_Person__S_age.
  337.  
  338. Set methods always have two inputs and one output. The first input value should be passed through as the output. The second output should be assigned the appropriate attribute of the input. Remember to decrement the use count of the attribute窶冱 old value and to increment the use count of the new value.
  339.  
  340.  C Representation:  
  341.  
  342.   C_class__S_id    
  343.    
  344.  Selector:   
  345.  
  346.   _C_class__S_id      
  347.   
  348.  Example:   
  349.  
  350.   /* Call the set method for attribute age of class Person */
  351.   C_Person *myPerson, *theInst;
  352.   C_integer *newage;    
  353.   
  354.   C_Person__S_age( myPerson, newage, &theInst);
  355.   
  356.  Example:  
  357.  
  358.   /* Assume a Prograph class Person with three attributes */
  359.   /* C Class declaration */
  360.  
  361.   typedef struct 
  362.   {      
  363.    Int2      type;    
  364.    Nat4      save;    
  365.    Nat4      use;    
  366.    C_string     *name;
  367.    C_string     *address;
  368.    C_integer    *age;
  369.   } *C_Person;
  370.  
  371.   /* Set method declaration */
  372.  
  373.   Nat2 C_Person__S_age( input1, input2, output )    
  374.   C_Person *input1;
  375.   C_integer *input2;
  376.   C_Person **output;
  377.   {
  378.    ...         /* Do Set stuff */
  379.    IncUse( input2);
  380.    IncUse( input1);      
  381.    DecUse( (**input1).age );
  382.    (**input1).age = input2;
  383.    *output = input1;
  384.    return PCF_TRUE;
  385.   }
  386.  
  387. Default Get and Set Methods *583*
  388.  
  389. Default get and set methods may be defined in C only, not in Prograph. A regular get or set selector is called when the name of an attribute is preceded by a slash in a get or set operation. A default get or set selector is called when the attribute name is not preceded by a slash. 
  390.  
  391. As described above default get and set selectors are also called by regular get and set selectors when no regular get or set method exists. 
  392.  
  393.  C Representation:  
  394.  
  395.   C_class__g_id      /* Default Get Method */
  396.   C_class__s_id      /* Default Set Method */  
  397.  
  398.  Object C Extensions  *583*
  399.  
  400. XCode can be written using the THINK C object extensions.  You should already have read the previous section which describes how to access and define Prograph language elements in non-object C. This section describes only the differences between object and non-object C.
  401.  
  402. NOTE:   Object C declarations for the Prograph data types are given in the section on Prograph data types.
  403.  
  404. Class identifiers do not have to be declared as external in object C. Also the double underscore at the end of class identifiers is omitted.
  405.  
  406.  Example:   
  407.  
  408.   /* Use the macro INCLASS to test if */
  409.   /* anInstance is of class Person */
  410.  
  411.   C_object *anInstance;
  412.    
  413.   if ( INCLASS( anInstance, C_Person) );    
  414.    ...
  415.  
  416. Instance attributes are accessed by defining a C object structure which parallels the Prograph class structure. Classes without ancestors in Prograph should inherit from the class C_object in C.
  417.  
  418.  Example:  
  419.  
  420.   /* Assume a class dog which inherits from class animal */
  421.   /* has been defined in Prograph */
  422.   
  423.   struct C_animal :   C_object 
  424.   {
  425.    C_integer    *age;      
  426.   };
  427.   
  428.   struct C_dog :   C_animal 
  429.   {    
  430.    C_string     *name;
  431.    C_string     *owner     
  432.   };
  433.  
  434.   /* Accessing attributes */
  435.   C_integer    *theAge;
  436.   C_string     *theName;
  437.   C_dog      *aDog;
  438.  
  439.   theAge = aDog->age;
  440.   theName = aDog->name;
  441.   ...
  442.  
  443. Prograph class methods are defined in the same way as object C methods. The 窶廚_class__窶 part of method names is omitted.
  444.  
  445.  Example:  
  446.  
  447.   /* Assume a Prograph class Person with three attributes */
  448.  
  449.   /* C Class declaration */
  450.   
  451.   struct C_Person :   C_object
  452.   {
  453.    C_string     *name;
  454.    C_string     *address;
  455.    C_integer    *age;
  456.  
  457.    /* Method Declarations must appear in class definition */
  458.    
  459.    Nat2 M_Add_20_Person( );      /* Class Method */
  460.  
  461.    Nat2 N_( C_Person** );       /* Initialization Method */
  462.    Nat2 G_age( C_Person**, C_integer** );    /* Get Method */
  463.    Nat2 S_age( C_integer*, C_Person** );     /* Set Method */    
  464.   }
  465.   
  466.   /* Method Body Declarations */
  467.   
  468.   /* Simple method  - one input, no outputs */
  469.  
  470.   Nat2 C_Person:  :  M_Add_20_Person(  )     
  471.   { ... }
  472.   
  473.   /* Initialization method - one input, one output */
  474.  
  475.   Nat2 C_Person:  :  N_( output )    
  476.   C_Person **output;
  477.   { 
  478.    ...          /* Do initialization stuff */
  479.    IncUse( this );
  480.    *output = this;
  481.    return PCF_TRUE 
  482.   }
  483.   
  484.   /* Get method - one input, two outputs */
  485.  
  486.   Nat2 C_Person:  :  G_age( output1, output2 )
  487.   C_Person **output1;
  488.   C_integer **output2;
  489.   {
  490.    ...         /* Do get stuff */
  491.    IncUse( this );
  492.    IncUse( this->age );
  493.    *output1 = this;
  494.    *output2 = this->age;
  495.    return PCF_TRUE;
  496.   }
  497.   
  498.   /* Set method  - two inputs, one output */
  499.  
  500.   Nat2 C_Person:  :  S_age( input, output )
  501.   C_integer *input;
  502.   C_Person **output;
  503.   {
  504.    ...         /* Do set stuff */    
  505.    IncUse( input );
  506.    IncUse( this );      
  507.    DecUse( this->age );
  508.    this->age = input;
  509.    *output = this;
  510.    return PCF_TRUE;
  511.   }
  512.  
  513. Method selectors are called using the THINK C method calling mechanism. Note that the calling instance is automatically passed as the first parameter to a selector. Methods can be called directly using the non-object C form described in the previous section.
  514.  
  515.  Example:  
  516.  
  517.   /* call the talk method of an animal */
  518.  
  519.   C_animal *myAnimal;
  520.  
  521.   myAnimal->M_talk( );
  522.   ...
  523.  
  524.  Building XCode in THINK C *586*
  525.  
  526. u Create a new THINK C project.
  527.  
  528. u    Include the header file X_includes.h in each of your C source files.
  529.  
  530. u Add your source files to the THINK C project.  
  531.  
  532. u Compile and build a library and save it in a file. 
  533.  
  534. u Include that file in your Prograph project.
  535.  
  536.  Building XCode in MPW C *586*
  537.  
  538. u    Copy the folder XCIncludes into your MPW Interfaces folder.
  539.  
  540. u    Insert the following lines into your MPW start up script and execute them:  
  541.  
  542.        Set XCIncludes "{MPW}Interfaces:  XCIncludes:  "
  543.    Export XCIncludes
  544.  
  545. u    Include the header file X_includes.h in each of your C source files.
  546.  
  547. u    Compile your source files with the command:  
  548.  
  549.   C mySource.c -i "{XCIncludes}"
  550.  
  551. u Include your object files and the library MPWLibrary in your Prograph project.
  552.